$(function () {

    $("#productInfo_datagrid").datagrid({
        url: '/product/selectProductList',
        title: '已购产品列表',
        width: 300,
        columns: [[
            {field: 'name', title: '险种', width: 130},
            {field: 'money', title: '险种金额', width: 130}
        ]]
    });

    $("#payBill_datagrid").datagrid({
        url: '/paymentBill/query?state=4',
        fit: true,
        toolbar: '#payBill_toolbar',
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        columns: [[
            {field: 'id', checkbox: true},
            {
                field: 'policyClient', title: '投保人', width: 40, formatter: function (value) {
                    return value.name;
                }
            },
            {field: 'number', title: '核保单号', width: 40},
            {field: 'price', title: '需缴纳金额', width: 80},
            {field: 'limitYear', title: '保险年限(/年)', width: 80},
            {field: 'underwritingDate', title: '核保日期', width: 40},
            {
                field: 'company', title: '保险机构', width: 40, formatter: function (value, row, index) {
                    return value.name;
                }
            },
            {
                field: 'state', title: '状态', width: 40, formatter: function (value, row, index) {
                    if(value == 4){
                        return "<font color='red'>已审核未缴费</font>";
                    }
                    if(value == 5){
                        return "<font color='green'>已缴费</font>";
                    }
                }
            }

        ]]
    });

    /*缴费处理单的弹窗*/
    $("#payBill_dialog").dialog({
        width: 350,
        height: 370,
        closed: true,
        modal: true,
        buttons: '#form_btn',
        onClose: function () {
            $("#payBill_form").form('clear');
        }
    });

    //所用的点击事件，都通过这个方法来调用对应的方法
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObject[cmd]();
    });

    var methodObject = {
        //刷新方法
        reload: function () {
            //鍒锋柊鏁版嵁琛ㄦ牸
            $("#payBill_datagrid").datagrid('reload');

        },
        //缴费方法
        payment: function () {
            var row = $("#payBill_datagrid").datagrid('getSelected');
            //如果没有选择缴费处理单的一条记录
            if (row == null) {
                $.messager.alert('温馨提示', '至少选择一条记录', 'error');
                return;
            }
            //处理缴费处理单名称不回显的问题
            if (row.policyClient) {
                row['policyClient.name'] = row.policyClient.name;
            }
            if (row.company) {
                row['company.name'] = row.company.name;
            }
            $("#productInfo_datagrid").datagrid('load', {
                number: row.number
            });
            $("#payBill_form").form('load', row);
            $("#payBill_dialog").dialog('setTitle', '编辑缴费处理单');
            $("#payBill_dialog").dialog('open');
        },
        //缴费提交
        submit: function () {
            $("#payBill_form").form('submit', {
                url: '/paymentBill/submit',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '缴费成功', 'info', function () {
                            $("#payBill_dialog").dialog('close');
                            $("#payBill_datagrid").datagrid('reload');

                        })
                    } else {
                        $.messager.alert('温馨提示', '缴费失败', 'error', function (r) {
                            if (r) {
                                $("#payBill_dialog").dialog('close');
                                $("#payBill_datagrid").datagrid('reload');
                            }
                        })
                    }
                }
            })
        },
        //取消方法
        cancel: function () {
            $("#payBill_dialog").dialog('close');
        },
        //多条件查询方法
        query: function () {
            var clientName = $("#applicantName").textbox('getValue');
            var sn = $("#number").textbox('getValue');
            $("#payBill_datagrid").datagrid('load', {
                clientName: clientName,
                sn: sn
            })
        }
    }
});